#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""

Code to read in datafiles from Beedle et al. 2021's study on the impact of temperature
gradients on the magnetopause current system. Reproduces table 1 from the paper using our dataset 
as well as percentage contributions of the divT and gradN components of the ion diamagnetic current
and percent comparisons of the total ion and electron diamangetic currents.

Requires the following files from the Harvard Dataverse database to run:
    
    MP_Crossing_ionData_561.txt
    MP_Crossing_elecData_561.txt

Created on Sat Sep 11, 2021

Modified on Mon Dec 13, 2021 

@author: Jason M. H. Beedle

Contact: (email) beedle@cua.edu 

"""

import numpy as np

#magnetopause crossing data storage class:
class magnetopauseCrossingData():
    
    def __init__(self,num):
        
        self.initial_num = num

#Calculates the magnitude given 3 component vectors:
def magnitudeCalc(x,y,z):
    
    mag = np.zeros(len(x))
    
    for i in range(len(x)):
        mag[i] = np.sqrt(x[i]**2+y[i]**2+z[i]**2)
    
    return mag

#Calcualtes the standard error of a given array using the standard deviation
def standErr(arr):
        
        num = len(arr)
        
        std = np.nanstd(arr)
        
        stanError = std/(np.sqrt(num))
            
        return stanError    

#Reads in the averaged ion data from the 561 MP crossings (MPC)
def ion_file_reader(mpd):

  ion_file = 'MP_Crossing_ionData_561.txt'  
    
  #Initializes data arrays:
  MP_ID = [] ; MP_Start = [] ; MP_End = [] #MPC ID number and MPC start and end time
  
  Ni = [] ; Ti_para = [] ; Ti_perp = [] #Ion plasma parameters
  
  Bx = [] ; By = [] ; Bz = [] #Magnetic field in Cartesian GSE coordinates (nT)
    
  CS_d = [] ; CS_Vn = []  #MPC duration (seconds) and normal velocity
  
  Xmp = [] ; Ymp = [] ; Zmp = []  #Location of MPC based on Cartesian GSE coordinates (RE)
  
  thetaMP = [] ; phiMP = [] #Location of MPC based on our phi and theta angles (in degrees)
  
  CST_km = [] ; CST_di = [] ; CST_rgi = [] #MPC thickness in (km), scaled to di, and scaled to rgi 
  
  di = [] ; rgi = [] #Ion plasma scales

  Jr_curl   = [] ; Jp_curl   = [] ; Jt_curl   = []  #Curlometer current
  Jr_totali = [] ; Jp_totali = [] ; Jt_totali = []  #Total ion diamagnetic current
  Jr_divTi  = [] ; Jp_divTi  = [] ; Jt_divTi  = []  #Divergence T ion diamagnetic current component
  Jr_gradNi = [] ; Jp_gradNi = [] ; Jt_gradNi = []  #Gradient N ion diamagnetic current component
  
  #Opens Ion data file
  with open (ion_file, "r") as file:
        data = file.readlines()
        
        for line in data: 
            
            sections = line.split()
            
            #Reads out header line:
            if sections[0] == '#':
                print("first line")
            
            #Reads in data to arrays
            else:
                MP_ID.append(sections[1])
                MP_Start.append(sections[2])
                MP_End.append(sections[3])
                
                CS_d.append(float(sections[4]))
                CS_Vn.append(float(sections[5]))
            
                Xmp.append(float(sections[6]))
                Ymp.append(float(sections[7]))
                Zmp.append(float(sections[8]))
            
                thetaMP.append(float(sections[9]))
                phiMP.append(float(sections[10]))
            
                CST_km.append(float(sections[11]))
                CST_di.append(float(sections[12]))
                CST_rgi.append(float(sections[13]))
                
                di.append(float(sections[14]))
                rgi.append(float(sections[15]))
                
                Jr_curl.append(float(sections[16]))
                Jp_curl.append(float(sections[17]))
                Jt_curl.append(float(sections[18]))
            
                Jr_totali.append(float(sections[19]))
                Jp_totali.append(float(sections[20]))
                Jt_totali.append(float(sections[21]))
            
                Jr_gradNi.append(float(sections[22]))
                Jp_gradNi.append(float(sections[23]))
                Jt_gradNi.append(float(sections[24]))
            
                Jr_divTi.append(float(sections[25]))
                Jp_divTi.append(float(sections[26]))
                Jt_divTi.append(float(sections[27]))
            
                Ti_para.append(float(sections[28]))
                Ti_perp.append(float(sections[29]))
                Ni.append(float(sections[30]))
            
                Bx.append(float(sections[31]))
                By.append(float(sections[32]))
                Bz.append(float(sections[33]))
                
   
  #stores ion data in mpd class:
  mpd.MP_ID_ion = MP_ID              
                
  mpd.CS_d  = CS_d
  mpd.CS_Vn = CS_Vn
                
  mpd.Xmp = Xmp
  mpd.Ymp = Ymp
  mpd.Zmp = Zmp
  
  mpd.thetaMP = thetaMP
  mpd.phiMP   = phiMP
  
  mpd.CST_km  = CST_km
  mpd.CST_di  = CST_di
  mpd.CST_rgi = CST_rgi
  
  mpd.di = di
  mpd.rgi = rgi
   
  mpd.Jr_curl = Jr_curl
  mpd.Jp_curl = Jp_curl
  mpd.Jt_curl = Jt_curl  
           
  mpd.Jr_totali = Jr_totali
  mpd.Jp_totali = Jp_totali
  mpd.Jt_totali = Jt_totali
  
  mpd.Jr_gradNi = Jr_gradNi
  mpd.Jp_gradNi = Jp_gradNi
  mpd.Jt_gradNi = Jt_gradNi
  
  mpd.Jr_divTi = Jr_divTi
  mpd.Jp_divTi = Jp_divTi
  mpd.Jt_divTi = Jt_divTi
  
  mpd.Ti_para = Ti_para
  mpd.Ti_perp = Ti_perp
  
  mpd.Ni = Ni 
  
  mpd.Bx = Bx 
  mpd.By = By
  mpd.Bz = Bz
              
  return   

#Reads in the averaged electron data from the 561 MP crossings (MPC)
def elec_file_reader(mpd):

  elec_file = 'MP_Crossing_elecData_561.txt'  
    
  #Initializes data arrays:
  MP_ID = [] 
  
  Ne = [] ; Te_para = [] ; Te_perp = [] #Electron plasma parameters
  
  de = [] ; rge = []  #Electron plasma scales
  
  Jr_totale = [] ; Jp_totale = [] ; Jt_totale = [] #Total elec diamagnetic current
  Jr_divTe  = [] ; Jp_divTe  = [] ; Jt_divTe  = [] #Divergence T elec diamagnetic current component
  Jr_gradNe = [] ; Jp_gradNe = [] ; Jt_gradNe = [] #Gradient N elec diamagnetic current component  
  
  #Opens Ion data file
  with open (elec_file, "r") as file:
        data = file.readlines()
        
        for line in data: 
            
            sections = line.split()
            
            #Reads out header line:
            if sections[0] == '#':
                print("first line")
            
            #Reads in data to arrays
            else:
                MP_ID.append(sections[1])
                de.append(float(sections[4]))
                rge.append(float(sections[5]))
                
                Jr_totale.append(float(sections[6]))
                Jp_totale.append(float(sections[7]))
                Jt_totale.append(float(sections[8]))
            
                Jr_gradNe.append(float(sections[9]))
                Jp_gradNe.append(float(sections[10]))
                Jt_gradNe.append(float(sections[11]))
            
                Jr_divTe.append(float(sections[12]))
                Jp_divTe.append(float(sections[13]))
                Jt_divTe.append(float(sections[14]))
            
                Te_para.append(float(sections[15]))
                Te_perp.append(float(sections[16]))
                Ne.append(float(sections[17]))
                
  #stores electron data in mpd class:
  mpd.MP_ID_elec = MP_ID
  mpd.Jr_totale = Jr_totale
  mpd.Jp_totale = Jp_totale
  mpd.Jt_totale = Jt_totale
  
  mpd.Jr_gradNe = Jr_gradNe
  mpd.Jp_gradNe = Jp_gradNe
  mpd.Jt_gradNe = Jt_gradNe
  
  mpd.Jr_divTe = Jr_divTe
  mpd.Jp_divTe = Jp_divTe
  mpd.Jt_divTe = Jt_divTe
  
  mpd.Te_para = Te_para
  mpd.Te_perp = Te_perp
  
  mpd.Ne = Ne

  return   

def mpSectorTable(mpd):
    
    #Sector Data arrays:
    dayJr_curl   = [] ; duskJr_curl   = [] ; dawnJr_curl   = []
    dayJr_totali = [] ; duskJr_totali = [] ; dawnJr_totali = []
    dayJr_gradNi = [] ; duskJr_gradNi = [] ; dawnJr_gradNi = []
    dayJr_divTi  = [] ; duskJr_divTi  = [] ; dawnJr_divTi  = []
    dayJr_totale = [] ; duskJr_totale = [] ; dawnJr_totale = []
    
    dayJp_curl   = [] ; duskJp_curl   = [] ; dawnJp_curl   = []
    dayJp_totali = [] ; duskJp_totali = [] ; dawnJp_totali = []
    dayJp_gradNi = [] ; duskJp_gradNi = [] ; dawnJp_gradNi = []
    dayJp_divTi  = [] ; duskJp_divTi  = [] ; dawnJp_divTi  = []
    dayJp_totale = [] ; duskJp_totale = [] ; dawnJp_totale = []
    
    dayJt_curl   = [] ; duskJt_curl   = [] ; dawnJt_curl   = []
    dayJt_totali = [] ; duskJt_totali = [] ; dawnJt_totali = []
    dayJt_gradNi = [] ; duskJt_gradNi = [] ; dawnJt_gradNi = []
    dayJt_divTi  = [] ; duskJt_divTi  = [] ; dawnJt_divTi  = []
    dayJt_totale = [] ; duskJt_totale = [] ; dawnJt_totale = []
    
    dayNi  = [] ; duskNi  = [] ; dawnNi  = []
    dayTi  = [] ; duskTi  = [] ; dawnTi  = []
    dayBx = [] ; duskBx = [] ; dawnBx = []
    dayBy = [] ; duskBy = [] ; dawnBy = []
    dayBz = [] ; duskBz = [] ; dawnBz = []
    
    dayCS_d  = [] ; duskCS_d  = [] ; dawnCS_d  = []
    dayCS_Vn = [] ; duskCS_Vn = [] ; dawnCS_Vn = []
    
    dayCS_km  = [] ; duskCS_km  = [] ; dawnCS_km  = []
    dayCS_rgi = [] ; duskCS_rgi = [] ; dawnCS_rgi = []
    dayCS_di  = [] ; duskCS_di  = [] ; dawnCS_di  = []
    
    #Groups MPCs (and their respective data) into arrays based on the MPC's location relative to the day, dusk, dawn sectors
    
    #The Dawn Flank is defined from -50 degrees to -90 degrees in phi
    #The Dusk Flank is defined from +50 degrees to +90 degrees in phi
    #The Dayside is defined as between +50 degrees to -50 degrees in phi
    
    #phiMP gives the MPC's location in degrees phi
    
    for i in range(len(mpd.phiMP)):
        if mpd.phiMP[i] > -50. and mpd.phiMP[i] < 50.:
            dayJr_curl.append(mpd.Jr_curl[i])
            dayJr_totali.append(mpd.Jr_totali[i])
            dayJr_gradNi.append(mpd.Jr_gradNi[i])
            dayJr_divTi.append(mpd.Jr_divTi[i])
            dayJr_totale.append(mpd.Jr_totale[i])
            
            dayJp_curl.append(mpd.Jp_curl[i])
            dayJp_totali.append(mpd.Jp_totali[i])
            dayJp_gradNi.append(mpd.Jp_gradNi[i])
            dayJp_divTi.append(mpd.Jp_divTi[i])
            dayJp_totale.append(mpd.Jp_totale[i])
            
            dayJt_curl.append(mpd.Jt_curl[i])
            dayJt_totali.append(mpd.Jt_totali[i])
            dayJt_gradNi.append(mpd.Jt_gradNi[i])
            dayJt_divTi.append(mpd.Jt_divTi[i])
            dayJt_totale.append(mpd.Jt_totale[i])
            
            dayNi.append(mpd.Ni[i])
            dayTi.append(mpd.Ti_perp[i]) 
            
            dayBx.append(mpd.Bx[i])
            dayBy.append(mpd.By[i])
            dayBz.append(mpd.Bz[i])
            
            dayCS_d.append(mpd.CS_d[i])
            dayCS_Vn.append(mpd.CS_Vn[i])
            
            dayCS_km.append(mpd.CST_km[i])
            dayCS_rgi.append(mpd.CST_rgi[i])
            dayCS_di.append(mpd.CST_di[i])
            
        elif mpd.phiMP[i] >= 50. and mpd.phiMP[i] <= 90.:
            duskJr_curl.append(mpd.Jr_curl[i])
            duskJr_totali.append(mpd.Jr_totali[i])
            duskJr_gradNi.append(mpd.Jr_gradNi[i])
            duskJr_divTi.append(mpd.Jr_divTi[i])
            duskJr_totale.append(mpd.Jr_totale[i])
            
            duskJp_curl.append(mpd.Jp_curl[i])
            duskJp_totali.append(mpd.Jp_totali[i])
            duskJp_gradNi.append(mpd.Jp_gradNi[i])
            duskJp_divTi.append(mpd.Jp_divTi[i])
            duskJp_totale.append(mpd.Jp_totale[i])
            
            duskJt_curl.append(mpd.Jt_curl[i])
            duskJt_totali.append(mpd.Jt_totali[i])
            duskJt_gradNi.append(mpd.Jt_gradNi[i])
            duskJt_divTi.append(mpd.Jt_divTi[i])
            duskJt_totale.append(mpd.Jt_totale[i])
            
            duskNi.append(mpd.Ni[i])
            duskTi.append(mpd.Ti_perp[i])
            
            duskBx.append(mpd.Bx[i])
            duskBy.append(mpd.By[i])
            duskBz.append(mpd.Bz[i])
            
            duskCS_d.append(mpd.CS_d[i])
            duskCS_Vn.append(mpd.CS_Vn[i])
            
            duskCS_km.append(mpd.CST_km[i])
            duskCS_rgi.append(mpd.CST_rgi[i])
            duskCS_di.append(mpd.CST_di[i])
            
            
        elif mpd.phiMP[i] <= -50. and mpd.phiMP[i] >= -90.:
            dawnJr_curl.append(mpd.Jr_curl[i])
            dawnJr_totali.append(mpd.Jr_totali[i])
            dawnJr_gradNi.append(mpd.Jr_gradNi[i])
            dawnJr_divTi.append(mpd.Jr_divTi[i])
            dawnJr_totale.append(mpd.Jr_totale[i])
            
            dawnJp_curl.append(mpd.Jp_curl[i])
            dawnJp_totali.append(mpd.Jp_totali[i])
            dawnJp_gradNi.append(mpd.Jp_gradNi[i])
            dawnJp_divTi.append(mpd.Jp_divTi[i])
            dawnJp_totale.append(mpd.Jp_totale[i])
            
            dawnJt_curl.append(mpd.Jt_curl[i])
            dawnJt_totali.append(mpd.Jt_totali[i])
            dawnJt_gradNi.append(mpd.Jt_gradNi[i])
            dawnJt_divTi.append(mpd.Jt_divTi[i])
            dawnJt_totale.append(mpd.Jt_totale[i])
            
            dawnNi.append(mpd.Ni[i])
            dawnTi.append(mpd.Ti_perp[i])
            
            dawnBx.append(mpd.Bx[i])
            dawnBy.append(mpd.By[i])
            dawnBz.append(mpd.Bz[i])
            
            dawnCS_d.append(mpd.CS_d[i])
            dawnCS_Vn.append(mpd.CS_Vn[i])
            
            dawnCS_km.append(mpd.CST_km[i])
            dawnCS_rgi.append(mpd.CST_rgi[i])
            dawnCS_di.append(mpd.CST_di[i])
        
    #Calculates mean values of each sector's values:
    meandayJr_curl   = np.nanmean(dayJr_curl)   ; meanduskJr_curl   = np.nanmean(duskJr_curl)   ; meandawnJr_curl   = np.nanmean(dawnJr_curl) 
    meandayJr_totali = np.nanmean(dayJr_totali) ; meanduskJr_totali = np.nanmean(duskJr_totali) ; meandawnJr_totali = np.nanmean(dawnJr_totali) 
    meandayJr_gradNi = np.nanmean(dayJr_gradNi) ; meanduskJr_gradNi = np.nanmean(duskJr_gradNi) ; meandawnJr_gradNi = np.nanmean(dawnJr_gradNi) 
    meandayJr_divTi  = np.nanmean(dayJr_divTi)  ; meanduskJr_divTi  = np.nanmean(duskJr_divTi)  ; meandawnJr_divTi  = np.nanmean(dawnJr_divTi) 
    meandayJr_totale = np.nanmean(dayJr_totale) ; meanduskJr_totale = np.nanmean(duskJr_totale) ; meandawnJr_totale = np.nanmean(dawnJr_totale)
    
    meandayJp_curl   = np.nanmean(dayJp_curl)   ; meanduskJp_curl   = np.nanmean(duskJp_curl)   ; meandawnJp_curl   = np.nanmean(dawnJp_curl) 
    meandayJp_totali = np.nanmean(dayJp_totali) ; meanduskJp_totali = np.nanmean(duskJp_totali) ; meandawnJp_totali = np.nanmean(dawnJp_totali) 
    meandayJp_gradNi = np.nanmean(dayJp_gradNi) ; meanduskJp_gradNi = np.nanmean(duskJp_gradNi) ; meandawnJp_gradNi = np.nanmean(dawnJp_gradNi) 
    meandayJp_divTi  = np.nanmean(dayJp_divTi)  ; meanduskJp_divTi  = np.nanmean(duskJp_divTi)  ; meandawnJp_divTi  = np.nanmean(dawnJp_divTi) 
    meandayJp_totale = np.nanmean(dayJp_totale) ; meanduskJp_totale = np.nanmean(duskJp_totale) ; meandawnJp_totale = np.nanmean(dawnJp_totale)
    
    meandayJt_curl   = np.nanmean(dayJt_curl)   ; meanduskJt_curl   = np.nanmean(duskJt_curl)   ; meandawnJt_curl   = np.nanmean(dawnJt_curl) 
    meandayJt_totali = np.nanmean(dayJt_totali) ; meanduskJt_totali = np.nanmean(duskJt_totali) ; meandawnJt_totali = np.nanmean(dawnJt_totali) 
    meandayJt_gradNi = np.nanmean(dayJt_gradNi) ; meanduskJt_gradNi = np.nanmean(duskJt_gradNi) ; meandawnJt_gradNi = np.nanmean(dawnJt_gradNi) 
    meandayJt_divTi  = np.nanmean(dayJt_divTi)  ; meanduskJt_divTi  = np.nanmean(duskJt_divTi)  ; meandawnJt_divTi  = np.nanmean(dawnJt_divTi) 
    meandayJt_totale = np.nanmean(dayJt_totale) ; meanduskJt_totale = np.nanmean(duskJt_totale) ; meandawnJt_totale = np.nanmean(dawnJt_totale)
    
    meandayNi  = np.nanmean(dayNi) ; meanduskNi  = np.nanmean(duskNi) ; meandawnNi  = np.nanmean(dawnNi)
    meandayTi  = np.nanmean(dayTi) ; meanduskTi  = np.nanmean(duskTi) ; meandawnTi  = np.nanmean(dawnTi)
    meandayBx = np.nanmean(dayBx); meanduskBx = np.nanmean(duskBx); meandawnBx = np.nanmean(dawnBx)
    meandayBy = np.nanmean(dayBy); meanduskBy = np.nanmean(duskBy); meandawnBy = np.nanmean(dawnBy)
    meandayBz = np.nanmean(dayBz); meanduskBz = np.nanmean(duskBz); meandawnBz = np.nanmean(dawnBz)
    
    meandayCS_d  = np.nanmean(dayCS_d)  ; meanduskCS_d   = np.nanmean(duskCS_d)  ; meandawnCS_d   = np.nanmean(dawnCS_d)
    meandayCS_Vn = np.nanmean(dayCS_Vn) ; meanduskCS_Vn  = np.nanmean(duskCS_Vn) ; meandawnCS_Vn  = np.nanmean(dawnCS_Vn)
    
    meandayCS_km  = np.nanmean(dayCS_km)  ; meanduskCS_km  = np.nanmean(duskCS_km)  ; meandawnCS_km  = np.nanmean(dawnCS_km)
    meandayCS_rgi = np.nanmean(dayCS_rgi) ; meanduskCS_rgi = np.nanmean(duskCS_rgi) ; meandawnCS_rgi = np.nanmean(dawnCS_rgi)
    meandayCS_di  = np.nanmean(dayCS_di)  ; meanduskCS_di  = np.nanmean(duskCS_di)  ; meandawnCS_di  = np.nanmean(dawnCS_di)
    
    #Calculates median values of each sector's values:
    mediandayJr_curl   = np.nanmedian(dayJr_curl)   ; medianduskJr_curl   = np.nanmedian(duskJr_curl)   ; mediandawnJr_curl   = np.nanmedian(dawnJr_curl) 
    mediandayJr_totali = np.nanmedian(dayJr_totali) ; medianduskJr_totali = np.nanmedian(duskJr_totali) ; mediandawnJr_totali = np.nanmedian(dawnJr_totali) 
    mediandayJr_gradNi = np.nanmedian(dayJr_gradNi) ; medianduskJr_gradNi = np.nanmedian(duskJr_gradNi) ; mediandawnJr_gradNi = np.nanmedian(dawnJr_gradNi) 
    mediandayJr_divTi  = np.nanmedian(dayJr_divTi)  ; medianduskJr_divTi  = np.nanmedian(duskJr_divTi)  ; mediandawnJr_divTi  = np.nanmedian(dawnJr_divTi) 
    mediandayJr_totale = np.nanmedian(dayJr_totale) ; medianduskJr_totale = np.nanmedian(duskJr_totale) ; mediandawnJr_totale = np.nanmedian(dawnJr_totale)
    
    mediandayJp_curl   = np.nanmedian(dayJp_curl)   ; medianduskJp_curl   = np.nanmedian(duskJp_curl)   ; mediandawnJp_curl   = np.nanmedian(dawnJp_curl) 
    mediandayJp_totali = np.nanmedian(dayJp_totali) ; medianduskJp_totali = np.nanmedian(duskJp_totali) ; mediandawnJp_totali = np.nanmedian(dawnJp_totali) 
    mediandayJp_gradNi = np.nanmedian(dayJp_gradNi) ; medianduskJp_gradNi = np.nanmedian(duskJp_gradNi) ; mediandawnJp_gradNi = np.nanmedian(dawnJp_gradNi) 
    mediandayJp_divTi  = np.nanmedian(dayJp_divTi)  ; medianduskJp_divTi  = np.nanmedian(duskJp_divTi)  ; mediandawnJp_divTi  = np.nanmedian(dawnJp_divTi) 
    mediandayJp_totale = np.nanmedian(dayJp_totale) ; medianduskJp_totale = np.nanmedian(duskJp_totale) ; mediandawnJp_totale = np.nanmedian(dawnJp_totale)
    
    mediandayJt_curl   = np.nanmedian(dayJt_curl)   ; medianduskJt_curl   = np.nanmedian(duskJt_curl)   ; mediandawnJt_curl   = np.nanmedian(dawnJt_curl) 
    mediandayJt_totali = np.nanmedian(dayJt_totali) ; medianduskJt_totali = np.nanmedian(duskJt_totali) ; mediandawnJt_totali = np.nanmedian(dawnJt_totali) 
    mediandayJt_gradNi = np.nanmedian(dayJt_gradNi) ; medianduskJt_gradNi = np.nanmedian(duskJt_gradNi) ; mediandawnJt_gradNi = np.nanmedian(dawnJt_gradNi) 
    mediandayJt_divTi  = np.nanmedian(dayJt_divTi)  ; medianduskJt_divTi  = np.nanmedian(duskJt_divTi)  ; mediandawnJt_divTi  = np.nanmedian(dawnJt_divTi) 
    mediandayJt_totale = np.nanmedian(dayJt_totale) ; medianduskJt_totale = np.nanmedian(duskJt_totale) ; mediandawnJt_totale = np.nanmedian(dawnJt_totale)
    
    mediandayNi  = np.nanmedian(dayNi) ; medianduskNi  = np.nanmedian(duskNi) ; mediandawnNi  = np.nanmedian(dawnNi)
    mediandayTi  = np.nanmedian(dayTi) ; medianduskTi  = np.nanmedian(duskTi) ; mediandawnTi  = np.nanmedian(dawnTi)
    mediandayBx = np.nanmedian(dayBx); medianduskBx = np.nanmedian(duskBx); mediandawnBx = np.nanmedian(dawnBx)
    mediandayBy = np.nanmedian(dayBy); medianduskBy = np.nanmedian(duskBy); mediandawnBy = np.nanmedian(dawnBy)
    mediandayBz = np.nanmedian(dayBz); medianduskBz = np.nanmedian(duskBz); mediandawnBz = np.nanmedian(dawnBz)
    
    mediandayCS_d  = np.nanmedian(dayCS_d)  ; medianduskCS_d   = np.nanmedian(duskCS_d)  ; mediandawnCS_d   = np.nanmedian(dawnCS_d)
    mediandayCS_Vn = np.nanmedian(dayCS_Vn) ; medianduskCS_Vn  = np.nanmedian(duskCS_Vn) ; mediandawnCS_Vn  = np.nanmedian(dawnCS_Vn)
    
    mediandayCS_km  = np.nanmedian(dayCS_km)  ; medianduskCS_km  = np.nanmedian(duskCS_km)  ; mediandawnCS_km  = np.nanmedian(dawnCS_km)
    mediandayCS_rgi = np.nanmedian(dayCS_rgi) ; medianduskCS_rgi = np.nanmedian(duskCS_rgi) ; mediandawnCS_rgi = np.nanmedian(dawnCS_rgi)
    mediandayCS_di  = np.nanmedian(dayCS_di)  ; medianduskCS_di  = np.nanmedian(duskCS_di)  ; mediandawnCS_di  = np.nanmedian(dawnCS_di)
    
    #Calculates magnitudes of the following quantities for each sector:
    magdayJcurl   = magnitudeCalc(dayJr_curl,dayJt_curl,dayJp_curl)
    magdayJtotali = magnitudeCalc(dayJr_totali,dayJt_totali,dayJp_totali)
    magdayJgradNi = magnitudeCalc(dayJr_gradNi,dayJt_gradNi,dayJp_gradNi)
    magdayJdivTi  = magnitudeCalc(dayJr_divTi,dayJt_divTi,dayJp_divTi)
    magdayJtotale = magnitudeCalc(dayJr_totale,dayJt_totale,dayJp_totale)
    
    magdayB = magnitudeCalc(dayBx,dayBy,dayBz)
    
    magduskJcurl   = magnitudeCalc(duskJr_curl,duskJt_curl,duskJp_curl)
    magduskJtotali = magnitudeCalc(duskJr_totali,duskJt_totali,duskJp_totali)
    magduskJgradNi = magnitudeCalc(duskJr_gradNi,duskJt_gradNi,duskJp_gradNi)
    magduskJdivTi  = magnitudeCalc(duskJr_divTi,duskJt_divTi,duskJp_divTi)
    magduskJtotale = magnitudeCalc(duskJr_totale,duskJt_totale,duskJp_totale)
    
    magduskB = magnitudeCalc(duskBx,duskBy,duskBz)
     
    magdawnJcurl   = magnitudeCalc(dawnJr_curl,dawnJt_curl,dawnJp_curl)
    magdawnJtotali = magnitudeCalc(dawnJr_totali,dawnJt_totali,dawnJp_totali)
    magdawnJgradNi = magnitudeCalc(dawnJr_gradNi,dawnJt_gradNi,dawnJp_gradNi)
    magdawnJdivTi  = magnitudeCalc(dawnJr_divTi,dawnJt_divTi,dawnJp_divTi)
    magdawnJtotale = magnitudeCalc(dawnJr_totale,dawnJt_totale,dawnJp_totale)
    
    magdawnB = magnitudeCalc(dawnBx,dawnBy,dawnBz)
    
    #Calculates magnitudes of mean vector quantities:
    meandayJcurl   = magnitudeCalc([meandayJr_curl],[meandayJt_curl],[meandayJp_curl])
    meandayJtotali = magnitudeCalc([meandayJr_totali],[meandayJt_totali],[meandayJp_totali])
    meandayJgradNi = magnitudeCalc([meandayJr_gradNi],[meandayJt_gradNi],[meandayJp_gradNi])
    meandayJdivTi  = magnitudeCalc([meandayJr_divTi],[meandayJt_divTi],[meandayJp_divTi])
    meandayJtotale = magnitudeCalc([meandayJr_totale],[meandayJt_totale],[meandayJp_totale])
    
    meanduskJcurl   = magnitudeCalc([meanduskJr_curl],[meanduskJt_curl],[meanduskJp_curl])
    meanduskJtotali = magnitudeCalc([meanduskJr_totali],[meanduskJt_totali],[meanduskJp_totali])
    meanduskJgradNi = magnitudeCalc([meanduskJr_gradNi],[meanduskJt_gradNi],[meanduskJp_gradNi])
    meanduskJdivTi  = magnitudeCalc([meanduskJr_divTi],[meanduskJt_divTi],[meanduskJp_divTi])
    meanduskJtotale = magnitudeCalc([meanduskJr_totale],[meanduskJt_totale],[meanduskJp_totale])
    
    meandawnJcurl   = magnitudeCalc([meandawnJr_curl],[meandawnJt_curl],[meandawnJp_curl])
    meandawnJtotali = magnitudeCalc([meandawnJr_totali],[meandawnJt_totali],[meandawnJp_totali])
    meandawnJgradNi = magnitudeCalc([meandawnJr_gradNi],[meandawnJt_gradNi],[meandawnJp_gradNi])
    meandawnJdivTi  = magnitudeCalc([meandawnJr_divTi],[meandawnJt_divTi],[meandawnJp_divTi])
    meandawnJtotale = magnitudeCalc([meandawnJr_totale],[meandawnJt_totale],[meandawnJp_totale]) 
    
    meandayB  = magnitudeCalc([meandayBx],[meandayBy],[meandayBz])
    meanduskB = magnitudeCalc([meanduskBx],[meanduskBy],[meanduskBz])
    meandawnB = magnitudeCalc([meandawnBx],[meandawnBy],[meandawnBz])
    
    #Calculates the standard error of the mean values in each sector (using the included error def standErr): 
    meandayJcurl_err   = standErr(magdayJcurl)  
    meandayJtotali_err = standErr(magdayJtotali)
    meandayJgradNi_err = standErr(magdayJgradNi)
    meandayJdivTi_err  = standErr(magdayJdivTi)
    meandayJtotale_err = standErr(magdayJtotale)
    
    meanduskJcurl_err   = standErr(magduskJcurl) 
    meanduskJtotali_err = standErr(magduskJtotali)
    meanduskJgradNi_err = standErr(magduskJgradNi)
    meanduskJdivTi_err  = standErr(magduskJdivTi)
    meanduskJtotale_err = standErr(magduskJtotale)
    
    meandawnJcurl_err   = standErr(magdawnJcurl) 
    meandawnJtotali_err = standErr(magdawnJtotali)
    meandawnJgradNi_err = standErr(magdawnJgradNi)
    meandawnJdivTi_err  = standErr(magdawnJdivTi)
    meandawnJtotale_err = standErr(magdawnJtotale)
    
    meandayJp_curl_err   = standErr(dayJp_curl)  
    meandayJp_totali_err = standErr(dayJp_totali)
    meandayJp_gradNi_err = standErr(dayJp_gradNi)
    meandayJp_divTi_err  = standErr(dayJp_divTi)
    meandayJp_totale_err = standErr(dayJp_totale)
    
    meanduskJp_curl_err   = standErr(duskJp_curl) 
    meanduskJp_totali_err = standErr(duskJp_totali)
    meanduskJp_gradNi_err = standErr(duskJp_gradNi)
    meanduskJp_divTi_err  = standErr(duskJp_divTi)
    meanduskJp_totale_err = standErr(duskJp_totale)
    
    meandawnJp_curl_err   = standErr(dawnJp_curl) 
    meandawnJp_totali_err = standErr(dawnJp_totali)
    meandawnJp_gradNi_err = standErr(dawnJp_gradNi)
    meandawnJp_divTi_err  = standErr(dawnJp_divTi)
    meandawnJp_totale_err = standErr(dawnJp_totale)
    
    meandayB_err  = standErr(magdayB)
    meanduskB_err = standErr(magduskB)
    meandawnB_err = standErr(magdawnB)
    
    meandayNi_err = standErr(dayNi)
    meandayTi_err = standErr(dayTi)
    
    meanduskNi_err = standErr(duskNi)
    meanduskTi_err = standErr(duskTi)
    
    meandawnNi_err = standErr(dawnNi)
    meandawnTi_err = standErr(dawnTi)
    
    meandayCS_d_err  = standErr(dayCS_d)
    meanduskCS_d_err = standErr(duskCS_d)
    meandawnCS_d_err = standErr(dawnCS_d)
    
    meandayCS_Vn_err  = standErr(dayCS_Vn)
    meanduskCS_Vn_err = standErr(duskCS_Vn)
    meandawnCS_Vn_err = standErr(dawnCS_Vn)
    
    meandayCS_km_err  = standErr(dayCS_km)
    meandayCS_rgi_err = standErr(dayCS_rgi)
    meandayCS_di_err  = standErr(dayCS_di)
    
    meanduskCS_km_err  = standErr(duskCS_km)
    meanduskCS_rgi_err = standErr(duskCS_rgi)
    meanduskCS_di_err  = standErr(duskCS_di)
    
    meandawnCS_km_err  = standErr(dawnCS_km)
    meandawnCS_rgi_err = standErr(dawnCS_rgi)
    meandawnCS_di_err  = standErr(dawnCS_di)
    
    #Calculates median magnitudes
    mediandayJcurl   = magnitudeCalc([mediandayJr_curl],[mediandayJt_curl],[mediandayJp_curl])
    mediandayJtotali = magnitudeCalc([mediandayJr_totali],[mediandayJt_totali],[mediandayJp_totali])
    mediandayJgradNi = magnitudeCalc([mediandayJr_gradNi],[mediandayJt_gradNi],[mediandayJp_gradNi])
    mediandayJdivTi  = magnitudeCalc([mediandayJr_divTi],[mediandayJt_divTi],[mediandayJp_divTi])
    mediandayJtotale = magnitudeCalc([mediandayJr_totale],[mediandayJt_totale],[mediandayJp_totale])
    
    medianduskJcurl   = magnitudeCalc([medianduskJr_curl],[medianduskJt_curl],[medianduskJp_curl])
    medianduskJtotali = magnitudeCalc([medianduskJr_totali],[medianduskJt_totali],[medianduskJp_totali])
    medianduskJgradNi = magnitudeCalc([medianduskJr_gradNi],[medianduskJt_gradNi],[medianduskJp_gradNi])
    medianduskJdivTi  = magnitudeCalc([medianduskJr_divTi],[medianduskJt_divTi],[medianduskJp_divTi])
    medianduskJtotale = magnitudeCalc([medianduskJr_totale],[medianduskJt_totale],[medianduskJp_totale])
    
    mediandawnJcurl   = magnitudeCalc([mediandawnJr_curl],[mediandawnJt_curl],[mediandawnJp_curl])
    mediandawnJtotali = magnitudeCalc([mediandawnJr_totali],[mediandawnJt_totali],[mediandawnJp_totali])
    mediandawnJgradNi = magnitudeCalc([mediandawnJr_gradNi],[mediandawnJt_gradNi],[mediandawnJp_gradNi])
    mediandawnJdivTi  = magnitudeCalc([mediandawnJr_divTi],[mediandawnJt_divTi],[mediandawnJp_divTi])
    mediandawnJtotale = magnitudeCalc([mediandawnJr_totale],[mediandawnJt_totale],[mediandawnJp_totale])
    
    mediandayB  = magnitudeCalc([mediandayBx],[mediandayBy],[mediandayBz])
    medianduskB = magnitudeCalc([medianduskBx],[medianduskBy],[medianduskBz])
    mediandawnB = magnitudeCalc([mediandawnBx],[mediandawnBy],[mediandawnBz])
    
    #Converts values to nanoAmpere/m^2
    meanduskJcurl = meanduskJcurl[0]*1000.
    meandayJcurl  = meandayJcurl[0]*1000.
    meandawnJcurl = meandawnJcurl[0]*1000.
    
    medianduskJcurl = medianduskJcurl[0]*1000.
    mediandayJcurl = mediandayJcurl[0]*1000.
    mediandawnJcurl = mediandawnJcurl[0]*1000.
    
    meanduskJcurl_err = meanduskJcurl_err*1000.
    meandayJcurl_err  = meandayJcurl_err*1000.
    meandawnJcurl_err = meandawnJcurl_err*1000.
    
    meanduskJtotali = meanduskJtotali[0]*1000. 
    meandayJtotali  = meandayJtotali[0]*1000.
    meandawnJtotali = meandawnJtotali[0]*1000.
    
    medianduskJtotali = medianduskJtotali[0]*1000.
    mediandayJtotali = mediandayJtotali[0]*1000.
    mediandawnJtotali = mediandawnJtotali[0]*1000.
    
    meanduskJtotali_err = meanduskJtotali_err*1000.
    meandayJtotali_err  = meandayJtotali_err*1000.
    meandawnJtotali_err = meandawnJtotali_err*1000.
    
    meanduskJtotale = meanduskJtotale[0]*1000.
    meandayJtotale  = meandayJtotale[0]*1000.
    meandawnJtotale = meandawnJtotale[0]*1000.
    
    medianduskJtotale = medianduskJtotale[0]*1000.
    mediandayJtotale = mediandayJtotale[0]*1000.
    mediandawnJtotale = mediandawnJtotale[0]*1000.
    
    meanduskJtotale_err = meanduskJtotale_err*1000.
    meandayJtotale_err  = meandayJtotale_err*1000. 
    meandawnJtotale_err = meandawnJtotale_err*1000.
    
    meanduskJgradNi = meanduskJgradNi[0]*1000.
    meandayJgradNi  = meandayJgradNi[0]*1000.
    meandawnJgradNi = meandawnJgradNi[0]*1000.
    
    medianduskJgradNi = medianduskJgradNi[0]*1000.
    mediandayJgradNi = mediandayJgradNi[0]*1000.
    mediandawnJgradNi = mediandawnJgradNi[0]*1000.
    
    meanduskJgradNi_err = meanduskJgradNi_err*1000.
    meandayJgradNi_err  = meandayJgradNi_err*1000.
    meandawnJgradNi_err = meandawnJgradNi_err*1000.
    
    meanduskJdivTi = meanduskJdivTi[0]*1000.
    meandayJdivTi  = meandayJdivTi[0]*1000.
    meandawnJdivTi = meandawnJdivTi[0]*1000.
    
    medianduskJdivTi = medianduskJdivTi[0]*1000.
    mediandayJdivTi  = mediandayJdivTi[0]*1000.
    mediandawnJdivTi = mediandawnJdivTi[0]*1000.
    
    meanduskJdivTi_err = meanduskJdivTi_err*1000.
    meandayJdivTi_err  = meandayJdivTi_err*1000.
    meandawnJdivTi_err = meandawnJdivTi_err*1000. 
    
    meanduskJp_curl = meanduskJp_curl*1000.
    meandayJp_curl  = meandayJp_curl*1000.
    meandawnJp_curl = meandawnJp_curl*1000.
    
    medianduskJp_curl = medianduskJp_curl*1000.
    mediandayJp_curl = mediandayJp_curl*1000.
    mediandawnJp_curl = mediandawnJp_curl*1000.
    
    meanduskJp_curl_err = meanduskJp_curl_err*1000.
    meandayJp_curl_err  = meandayJp_curl_err*1000.
    meandawnJp_curl_err = meandawnJp_curl_err*1000.
    
    meanduskJp_totali = meanduskJp_totali*1000. 
    meandayJp_totali  = meandayJp_totali*1000.
    meandawnJp_totali = meandawnJp_totali*1000.
    
    meanduskJp_totale = meanduskJp_totale*1000. 
    meandayJp_totale  = meandayJp_totale*1000.
    meandawnJp_totale = meandawnJp_totale*1000.
    
    medianduskJp_totali = medianduskJp_totali*1000.
    mediandayJp_totali = mediandayJp_totali*1000.
    mediandawnJp_totali = mediandawnJp_totali*1000.
    
    meanduskJp_totali_err = meanduskJp_totali_err*1000.
    meandayJp_totali_err  = meandayJp_totali_err*1000.
    meandawnJp_totali_err = meandawnJp_totali_err*1000.
    
    meanduskJp_totale_err = meanduskJp_totale_err*1000.
    meandayJp_totale_err  = meandayJp_totale_err*1000. 
    meandawnJp_totale_err = meandawnJp_totale_err*1000.
    
    meanduskJp_gradNi = meanduskJp_gradNi*1000.
    meandayJp_gradNi  = meandayJp_gradNi*1000.
    meandawnJp_gradNi = meandawnJp_gradNi*1000.
    
    medianduskJp_gradNi = medianduskJp_gradNi*1000.
    mediandayJp_gradNi = mediandayJp_gradNi*1000.
    mediandawnJp_gradNi = mediandawnJp_gradNi*1000.
    
    meanduskJp_gradNi_err = meanduskJp_gradNi_err*1000.
    meandayJp_gradNi_err  = meandayJp_gradNi_err*1000.
    meandawnJp_gradNi_err = meandawnJp_gradNi_err*1000.
    
    meanduskJp_divTi = meanduskJp_divTi*1000.
    meandayJp_divTi  = meandayJp_divTi*1000.
    meandawnJp_divTi = meandawnJp_divTi*1000.
    
    medianduskJp_divTi = medianduskJp_divTi*1000.
    mediandayJp_divTi  = mediandayJp_divTi*1000.
    mediandawnJp_divTi = mediandawnJp_divTi*1000.
    
    meanduskJp_divTi_err = meanduskJp_divTi_err*1000.
    meandayJp_divTi_err  = meandayJp_divTi_err*1000.
    meandawnJp_divTi_err = meandawnJp_divTi_err*1000. 
    
    meanduskB = meanduskB[0]
    meandayB  = meandayB[0]
    meandawnB = meandawnB[0]
    
    medianduskB = medianduskB[0]
    mediandayB  = mediandayB[0]
    mediandawnB = mediandawnB[0]
    
    #Calculates the percent contribution of the density and temperature diamagnetic current components to
    #the total diamagnetic current in the phi direction (or along the magnetopause)
    
    dayDia_p  = abs(meandayJp_gradNi)  + abs(meandayJp_divTi)
    duskDia_p = abs(meanduskJp_gradNi) + abs(meanduskJp_divTi)
    dawnDia_p = abs(meandawnJp_gradNi) + abs(meandawnJp_divTi)

    daypercentDivTi_p  = (abs(meandayJp_divTi)/dayDia_p) * 100.
    daypercentGradNi_p = (abs(meandayJp_gradNi)/dayDia_p) * 100.
    
    duskpercentDivTi_p  = (abs(meanduskJp_divTi)/duskDia_p) * 100.
    duskpercentGradNi_p = (abs(meanduskJp_gradNi)/duskDia_p) * 100.
    
    dawnpercentDivTi_p  = (abs(meandawnJp_divTi)/dawnDia_p) * 100.
    dawnpercentGradNi_p = (abs(meandawnJp_gradNi)/dawnDia_p) * 100.
    
    #Calculates what percent the electron diamagnetic current denisty is of the ion diamagnetic current density
    #in the phi direction along the day, dusk, and dawn sectors
    
    dayPercentIonElec = abs(meandayJp_totale/meandayJp_totali) * 100. 
    duskPercentIonElec = abs(meanduskJp_totale/meanduskJp_totali) * 100. 
    dawnPercentIonElec = abs(meandawnJp_totale/meandawnJp_totali) * 100. 
    
    #Saves data to a .txt file: 
    
    f = "database_table_ion_" + str(len(mpd.phiMP))+".txt"

    table = open(f,'w')

    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Parameters","Dusk","Dayside","Dawn"))
    table.write("{: <25} {: <25.1f} {: <25.1f} {: <25.1f}\n".format("Crossings",len(duskJr_curl),len(dayJr_curl),len(dawnJr_curl)))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Thickness (km)",str('%.1f'%(meanduskCS_km))+'('+str('%.1f'%(medianduskCS_km)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskCS_km_err)) , str('%.1f'%(meandayCS_km))+'('+str('%.1f'%(mediandayCS_km)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayCS_km_err)) , str('%.1f'%(meandawnCS_km))+'('+str('%.1f'%(mediandawnCS_km)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnCS_km_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Thickness (d)",str('%.1f'%(meanduskCS_di))+'('+str('%.1f'%(medianduskCS_di)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskCS_di_err)) , str('%.1f'%(meandayCS_di))+'('+str('%.1f'%(mediandayCS_di)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayCS_di_err)) , str('%.1f'%(meandawnCS_di))+'('+str('%.1f'%(mediandawnCS_di)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnCS_di_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Thickness (rgi)",str('%.1f'%(meanduskCS_rgi))+'('+str('%.1f'%(medianduskCS_rgi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskCS_rgi_err)) , str('%.1f'%(meandayCS_rgi))+'('+str('%.1f'%(mediandayCS_rgi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayCS_rgi_err)) , str('%.1f'%(meandawnCS_rgi))+'('+str('%.1f'%(mediandawnCS_rgi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnCS_rgi_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Vn (km/s)",str('%.1f'%(meanduskCS_Vn))+'('+str('%.1f'%(medianduskCS_Vn)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskCS_Vn_err)) , str('%.1f'%(meandayCS_Vn))+'('+str('%.1f'%(mediandayCS_Vn)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayCS_Vn_err)) , str('%.1f'%(meandawnCS_Vn))+'('+str('%.1f'%(mediandawnCS_Vn)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnCS_Vn_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("MPC Duration (s)",str('%.1f'%(meanduskCS_d))+'('+str('%.1f'%(medianduskCS_d)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskCS_d_err)) , str('%.1f'%(meandayCS_d))+'('+str('%.1f'%(mediandayCS_d)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayCS_d_err)) , str('%.1f'%(meandawnCS_d))+'('+str('%.1f'%(mediandawnCS_d)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnCS_d_err))))
    
    table.write(" \n")
    
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("|Jcurl| (nA/m^2)",str('%.1f'%(meanduskJcurl))+'('+str('%.1f'%(medianduskJcurl)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskJcurl_err)) , str('%.1f'%(meandayJcurl))+'('+str('%.1f'%(mediandayJcurl)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayJcurl_err)) , str('%.1f'%(meandawnJcurl))+'('+str('%.1f'%(mediandawnJcurl)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnJcurl_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("|Jtotali| (nA/m^2)",str('%.1f'%(meanduskJtotali))+'('+str('%.1f'%(medianduskJtotali)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskJtotali_err)) , str('%.1f'%(meandayJtotali))+'('+str('%.1f'%(mediandayJtotali)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayJtotali_err)) , str('%.1f'%(meandawnJtotali))+'('+str('%.1f'%(mediandawnJtotali)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnJtotali_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("|JgradNi| (nA/m^2)",str('%.1f'%(meanduskJgradNi))+'('+str('%.1f'%(medianduskJgradNi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskJgradNi_err)) , str('%.1f'%(meandayJgradNi))+'('+str('%.1f'%(mediandayJgradNi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayJgradNi_err)) , str('%.1f'%(meandawnJgradNi))+'('+str('%.1f'%(mediandawnJgradNi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnJgradNi_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("|JdivTi| (nA/m^2)",str('%.1f'%(meanduskJdivTi))+'('+str('%.1f'%(medianduskJdivTi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskJdivTi_err)) , str('%.1f'%(meandayJdivTi))+'('+str('%.1f'%(mediandayJdivTi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayJdivTi_err)) , str('%.1f'%(meandawnJdivTi))+'('+str('%.1f'%(mediandawnJdivTi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnJdivTi_err))))
    
    table.write(" \n")
    
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Jcurl ɸ (nA/m^2)",str('%.1f'%(meanduskJp_curl))+'('+str('%.1f'%(medianduskJp_curl)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskJp_curl_err)) , str('%.1f'%(meandayJp_curl))+'('+str('%.1f'%(mediandayJp_curl)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayJp_curl_err)) , str('%.1f'%(meandawnJp_curl))+'('+str('%.1f'%(mediandawnJp_curl)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnJp_curl_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Jtotali ɸ (nA/m^2)",str('%.1f'%(meanduskJp_totali))+'('+str('%.1f'%(medianduskJp_totali)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskJp_totali_err)) , str('%.1f'%(meandayJp_totali))+'('+str('%.1f'%(mediandayJp_totali)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayJp_totali_err)) , str('%.1f'%(meandawnJp_totali))+'('+str('%.1f'%(mediandawnJp_totali)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnJp_totali_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("JgradNi ɸ (nA/m^2)",str('%.1f'%(meanduskJp_gradNi))+'('+str('%.1f'%(medianduskJp_gradNi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskJp_gradNi_err)) , str('%.1f'%(meandayJp_gradNi))+'('+str('%.1f'%(mediandayJp_gradNi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayJp_gradNi_err)) , str('%.1f'%(meandawnJp_gradNi))+'('+str('%.1f'%(mediandawnJp_gradNi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnJp_gradNi_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("JdivTi ɸ (nA/m^2)",str('%.1f'%(meanduskJp_divTi))+'('+str('%.1f'%(medianduskJp_divTi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskJp_divTi_err)) , str('%.1f'%(meandayJp_divTi))+'('+str('%.1f'%(mediandayJp_divTi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayJp_divTi_err)) , str('%.1f'%(meandawnJp_divTi))+'('+str('%.1f'%(mediandawnJp_divTi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnJp_divTi_err))))
    
    
    table.write(" \n")
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("|Jtotale| (nA/m^2)",str('%.1f'%(meanduskJtotale))+'('+str('%.1f'%(medianduskJtotale)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskJtotale_err)) , str('%.1f'%(meandayJtotale))+'('+str('%.1f'%(mediandayJtotale)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayJtotale_err)) , str('%.1f'%(meandawnJtotale))+'('+str('%.1f'%(mediandawnJtotale)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnJtotale_err))))
    table.write(" \n") 
    
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Ti Perp (eV)",str('%.1f'%(meanduskTi))+'('+str('%.1f'%(medianduskTi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskTi_err)) , str('%.1f'%(meandayTi))+'('+str('%.1f'%(mediandayTi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayTi_err)) , str('%.1f'%(meandawnTi))+'('+str('%.1f'%(mediandawnTi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnTi_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Ni (cm^-3)",str('%.1f'%(meanduskNi))+'('+str('%.1f'%(medianduskNi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskNi_err)) , str('%.1f'%(meandayNi))+'('+str('%.1f'%(mediandayNi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayNi_err)) , str('%.1f'%(meandawnNi))+'('+str('%.1f'%(mediandawnNi)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnNi_err))))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("|B| (nT)",str('%.1f'%(meanduskB))+'('+str('%.1f'%(medianduskB)) +') ' + u"\u00B1" + " " + str('%.1f'%(meanduskB_err)) , str('%.1f'%(meandayB))+'('+str('%.1f'%(mediandayB)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandayB_err)) , str('%.1f'%(meandawnB))+'('+str('%.1f'%(mediandawnB)) +') ' + u"\u00B1" + " " + str('%.1f'%(meandawnB_err))))
    
    table.write(" \n") 
    
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Percent ɸ divTi",str('%.1f'%(duskpercentDivTi_p))+'%',str('%.1f'%(daypercentDivTi_p))+'%',str('%.1f'%(dawnpercentDivTi_p))+'%'))
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Percent ɸ gradNi",str('%.1f'%(duskpercentGradNi_p))+'%',str('%.1f'%(daypercentGradNi_p))+'%',str('%.1f'%(dawnpercentGradNi_p))+'%'))
    
    table.write(" \n") 
    
    table.write("{: <25} {: <25} {: <25} {: <25}\n".format("Percent ɸ Elec / Ion ",str('%.1f'%(duskPercentIonElec))+'%',str('%.1f'%(dayPercentIonElec))+'%',str('%.1f'%(dawnPercentIonElec))+'%'))
     
    table.close()             
    
    return

#Number of crossings included in our study:  
crossing_num = 561

#Initializes class:
mpd = magnetopauseCrossingData(crossing_num)

#Reads in the ion and electron data:
ion_file_reader(mpd)
elec_file_reader(mpd)

#Creates the table and contribution percentages
mpSectorTable(mpd)





